import api from '@/api';

export default {
  namespace: 'balance',

  state: {
    accountList: {
      list: [],
      pagination: {},
    },
    currentAccount: {},
    tradingFlows: {
      list: [],
      pagination: {},
    },
    selectedCus: [],
  },

  effects: {
    *fetchAccounts({ payload }, { call, put }) {
      let res = yield call(api.balance.queryAccount, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveAccounts',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
    },
    *fetchCurrent({ payload }, { call, put }) {
      const response = yield call(api.balance.queryCurrent, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveCurrent',
          payload: response.data,
        });
      }
      return response;
    },

    *fetchTradingFlow({ payload }, { call, put }) {
      let res = yield call(api.balance.queryTradingFlow, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveTradingFlows',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
    },
  },

  reducers: {
    saveAccounts(state, { payload }) {
      return {
        ...state,
        accountList: payload,
      };
    },
    saveCurrent(state, { payload }) {
      return {
        ...state,
        currentAccount: payload,
      };
    },
    clearCurrent(state) {
      return {
        ...state,
        currentAccount: {},
      };
    },
    saveTradingFlows(state, { payload }) {
      return {
        ...state,
        tradingFlows: payload,
      };
    },
    saveSelectedCus(state, { payload }) {
      return {
        ...state,
        selectedCus: payload,
      };
    },
  },
};
